/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigs.web.common;

import java.io.IOException;
import java.io.InputStream;

import org.apache.wicket.Resource;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.calldecorator.AjaxPreprocessingCallDecorator;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.util.resource.AbstractResourceStream;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.ResourceStreamNotFoundException;

import brix.BrixNodeModel;
import brix.jcr.wrapper.BrixFileNode;
import brix.jcr.wrapper.BrixNode;

import com.googlecode.chigs.web.ChigsApplication;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
public abstract class AActionLink extends Panel {
	private final StringResourceModel message;

	public AActionLink(boolean ajax) {
		this(ajax, null);
	}

	public AActionLink(boolean ajax, StringResourceModel message) {
		super("item");
		this.message = message;
		if (ajax) {
			ajaxInit();
		} else {
			init();
		}
	}

	private void ajaxInit() {
		AjaxFallbackLink<Object> link = new AjaxFallbackLink<Object>("link") {
			@Override
			public void onClick(AjaxRequestTarget target) {
				AActionLink.this.onClick(target);
			}
			@Override
			protected IAjaxCallDecorator getAjaxCallDecorator()
			{
				return new AjaxPreprocessingCallDecorator(super.getAjaxCallDecorator())
				{
					@Override
					public CharSequence preDecorateScript(CharSequence script)
					{
						return message == null ? script : "if(!confirm('" + message.getString() + "')) return false;" + script;
					}
				};
			}
		};
		link.add(getImage());
		add(link);
	}

	protected Image getImage() {
		Image img = new Image("icon", new Resource() {
			@Override
			public IResourceStream getResourceStream() {
				return new AbstractResourceStream() {
					// must be transient, the stream itself is not serializeable
					private transient InputStream stream;

					@Override
					public void close() throws IOException {
						stream.close();
					}

					@Override
					public InputStream getInputStream() throws ResourceStreamNotFoundException {
						BrixNodeModel model = ((ChigsApplication) getApplication()).getBrixLink("/" + getIcon().getName());
						BrixNode node = model.getObject();
						BrixFileNode bfn = new BrixFileNode(node, node.getSession());
						stream = bfn.getDataAsStream();
						return stream;
					}

				};
			}
		}); 
		return img;
	}

	private void init() {
		Link<Object> link = new Link<Object>("link") {
			@Override
			public void onClick() {
				AActionLink.this.onClick(null);
			}
		};
		if (message!=null) {
			link.add(new SimpleAttributeModifier("onclick", "return confirm('" + message.getString() + "');"));
		}
		link.add(getImage());
		add(link);
	}

	protected IAjaxCallDecorator getAjaxCallDecorator() {
		return null;
	}

	abstract public void onClick(AjaxRequestTarget target);

	abstract ResourceReference getIcon();
}
